﻿using Gameserver.Cache;
using Protocol.DTO;
using ServerFrame;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gameserver.Biz
{
    public class UserBiz
    {
        public UserCache cache;
        public UserBiz()
        {
            cache = CacheFactory.userCache;
        }
        /// <summary>
        /// 登陆事务
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="token"></param>
        /// <returns>
        /// 0 登陆成功
        /// 1 账号不存在
        /// 2 账号已上线
        /// 3 密码不正确
        /// </returns>
        public int LoginBiz(User_DTO dto,Mytoken token)
        {
            if(!cache.hasUser(dto.username))
            {
                return 1;
            }
            if (cache.isOnline(dto.username))
            {
                return 2;
            }
            if(!cache.Match(dto.username,dto.password))
            {
                return 3;
            }
            cache.Online(dto.username, token);
            Console.WriteLine(dto.username + "上线");
            return 0;
        }
        /// <summary>
        /// 注册事务
        /// </summary>
        /// <param name="dto"></param>
        /// <returns>
        /// 0 注册成功
        /// 1 账号已存在
        /// </returns>
        public int RegBiz(User_DTO dto)
        {
            //todo 判断name是否符合规范
            if(cache.hasUser(dto.username))
            {
                return 1;
            }
            cache.Create(dto.username, dto.password);
            return 0;
        }

        public void ClientClose(Mytoken token)
        {
            if (cache.isOnline(token))
            {
                cache.Offline(token);
                Console.WriteLine("客户端下线");
            }
        }

        
    }
}
